import pandas as pd


class Counter:
    def __init__(self):
        self.json = pd.read_json("count.json")
        self.classes = self.json["classes"][0]
        self.count_data = self.json["data"]
        self.id = 1
        self.type = 1

        print("Counter version: ", self.json["version"][0])
        print("Counter classes: ", self.json["classes"][0])
        self.ifg = 0
        self.stage_ifg = True

    def __call__(self, *args, **kwargs):
        pass

    def count(self, angle):
        for i in self.step[self.step_key[self.ifg]]:
            # print(self.step[self.step_key[self.ifg]][i])
            if self.step[self.step_key[self.ifg]][i][1] == "l":
                if angle[int(i)] < self.step[self.step_key[self.ifg]][i][0]:
                    continue
                else:
                    return 0
            else:
                if angle[int(i)] > self.step[self.step_key[self.ifg]][i][0]:
                    continue
                else:
                    return 0
        self.ifg += 1
        if self.ifg == self.count_data[self.id]["type"] + 1:
            self.ifg = 0
            return 1
        return 0


    def get_id(self, id):
        self.id = "class_{}".format(id)
        for i in self.count_data:
            if i["id"] == id:
                self.type = i["type"]
        self.step = self.count_data[self.id]["step"]
        self.angle = self.count_data[self.id]["angle"]
        self.step_key = list(self.step.keys())



if __name__ == '__main__':
    C = Counter()
    C.get_id(1)
    C.count(1)
